package com.huike.web.controller.system;

import com.huike.clues.domain.SysDictType;
import com.huike.clues.service.ISysDictTypeService;
import com.huike.common.constant.UserConstants;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.utils.SecurityUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/dict/type")
@Api(tags ="数据字典类型信息")
@Slf4j
public class SysDictTypeController extends BaseController {
    @Autowired
    private ISysDictTypeService dictTypeService;

    /**
     * 分页查询数据字典类型信息
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDictType dictType) {
        startPage();
        List<SysDictType> list = dictTypeService.selectDictTypeList(dictType);
        return getDataTablePage(list);
    }

    /**
     * 查询字典类型详细信息
     */
    @GetMapping("/{dictId}")
    public AjaxResult getInfo(@PathVariable Long dictId) {
        SysDictType dict = dictTypeService.selectDictTypeById(dictId);
        return AjaxResult.success(dict);
    }

    /**
     * 修改字典类型
     */
    @PutMapping()
    public AjaxResult update(@RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return AjaxResult.error("修改字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setUpdateBy(SecurityUtils.getUsername());
        dictTypeService.updateDictType(dict);
        return AjaxResult.success();
    }
    /**
     * 新增字典类型
     */
    @PostMapping()
    public AjaxResult add(@RequestBody SysDictType dict) {
        if (UserConstants.NOT_UNIQUE.equals(dictTypeService.checkDictTypeUnique(dict))) {
            return AjaxResult.error("新增字典'" + dict.getDictName() + "'失败，字典类型已存在");
        }
        dict.setCreateBy(SecurityUtils.getUsername());
        dictTypeService.insertDictType(dict);
        return AjaxResult.success();
    }

    /**
     * 删除字典类型
     */
    @DeleteMapping("/{dictIds}")
    public AjaxResult remove(@PathVariable Long[] dictIds) {
        dictTypeService.deleteDictTypeByIds(dictIds);
        return AjaxResult.success();
    }
}
